<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>子父传值</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>

</head>

<body>
    <div id="app">
        <p>{{count}}</p>
        <my-counter v-on:add="add1" :msg="msg"></my-counter>
    </div>
    <script>
        Vue.component("my-counter", {
            props:['msg'],
            data(){
                return{
                    son:"子组件"
                }
            },
            template: `
            <div>            
                <p>{{son}}</p>
                <p>{{msg}}</p>
                 <button v-on:click="btnClick">增加</button>
            </div>
           `,
           beforeCreate() {
                console.log("子组件的beforeCreated");
            },
            created(){
                console.log("子组件的created");
            },
            beforeMount() {
                console.log("子组件的beforeMount");
            },
            mounted() {
                console.log("子组件的mounted");
            },
            beforeUpdate() {
                console.log('子组件的beforeUpdate');
            },
            updated() {
                console.log("子组件的updated");
            },
            beforeDestroy() {
                console.log("子组件的beforeDestroy");
            },
            destroyed() {
                console.log("子组件的destroyed");
            },

           methods: {
               btnClick(){
                console.log("子组件的btn事件");
                this.$emit("add",{
                    addCount:10,
                    name:"二蛋"
                });
               }
           },
        })

        var app = new Vue({
            el: "#app",
            data: {
                count: 0,
                msg:"父子传值"
            },
            beforeCreate() {
                console.log("父组件的beforeCreated");
            },
            created(){
                console.log("父组件的created");
            },
            beforeMount() {
                console.log("父组件的beforeMount");
            },
            mounted() {
                console.log("父组件的mounted");
            },
            beforeUpdate() {
                console.log('父组件的beforeUpdate');
            },
            updated() {
                console.log("父组件的updated");
            },
            beforeDestroy() {
                console.log("父组件的beforeDestroy");
            },
            destroyed() {
                console.log("父组件的destroyed");
            },

            methods: {
                add1(e){
                    console.log(e.name);
                    console.log("根组件count++");
                    this.count+=e.addCount;
                }
            },

        })

    </script>
</body>

</html>